<template>
    <div class="login-container">
        <div class="login-left">
            <img src="../assets/imgs/login_img.png">
        </div>
        <div class="login-box">
            <!-- 头像 -->
            <div class="avatar-box">
                <img src="../assets/imgs/login_logo.png">
            </div>
            <!-- 标题 -->
            <div class="title">
                商城后台管理系统
            </div>
            <!-- 登陆表单 -->
            <el-form ref="loginFormRef" class="login-form" :model="loginForm" :rules="loginFormRules">
                <el-form-item prop="mobile">
                    <el-input placeholder="请输入手机号" prefix-icon="iconfont icon-user" v-model="loginForm.mobile"></el-input>
                </el-form-item>
                <el-form-item prop="code">
                    <el-input placeholder="请输入验证码" prefix-icon="iconfont icon-3702mima" style="width:130px;" v-model="loginForm.code" @keyup.enter.native="login"></el-input>
                    <el-button type="warning" @click="loginCode" style="width:120px;margin-left:8px;">获取验证码</el-button>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" class="login-btn" @click="login">登陆</el-button>
                </el-form-item>
            </el-form>
        </div>
    </div>
</template>

<script>
import Api from '@/api/Api.js'
export default {
    data(){
        return {
            // 表单的数据绑定对象
            loginForm:{
                 mobile:'',
                 code:''
            },
            // 表单的校验规则
            loginFormRules:{
                mobile: [
                    { required: true, message: '请输入手机号', trigger: 'blur' },
                    { max: 11, message: '长度11个字符', trigger: 'blur' }
                ],
                code:[
                    { required: true, message: '请输入验证码', trigger: 'blur' }
                ]
            }
        }
    },
    created() {
        this.autoLogin()
    },
    methods:{
        loginCode() {
            Api.getLoginCode(this.loginForm).then(res => {
                    if (res.success) {
                        this.$message.success('发送成功')
                    } else {
                        this.$message.error('发送失败：' + res.msg)
                    }
            })
        },
        autoLogin() {
            var uuid = localStorage.getItem('uuid');
            if(uuid!=''){
                Api.autoLogin(this.loginForm).then(res => {
                    if (res.success) {
                       this.$router.push('/home')
                    }
                })
            }
        },
        login(){
            // 判断是否校验成功
            this.$refs.loginFormRef.validate(async valid => {
                if(!valid){
                    return
                }

                Api.login(this.loginForm).then(res => {
                    if (res.success) {
                        this.$message.success('登陆成功')
                        // 将用户信息保存到sessionStorage中
                        localStorage.setItem("userId", res.data.userId);
                        localStorage.setItem('uuid',res.data.uuid);
                        localStorage.setItem('username',res.data.username);
                        this.$router.push('/home')
                    } else {
                        this.$message.error('登录失败：' + res.msg)
                    }
                })
            })
        }
    }
}
</script>

<style lang="less" scoped>
.login-container{
    background: #93defe;
    height: 100%;
    display: flex;
    justify-content: center;
    align-items: center;

    .login-left{
        margin-right: 100px;
    }

    .login-box{
        width: 400px;
        height: 350px;
        background: #fff;
        padding: 50px;
        border-radius: 6px;
        box-sizing: border-box;
        position: relative;

        .avatar-box{
            width: 120px;
            height: 120px;
            border: 5px solid #93defe;
            border-radius: 100px;
            text-align: center;
            line-height: 110px;
            position: absolute;
            top: -60px;
            right: 140px;
            background: #fff;
        }

        .title{
            position: absolute;
            top: 100px;
            left:50%;
            transform: translate(-50%);
            font-size: 18px;
            color: #444;
        }

        .login-form{
            position: absolute;
            bottom: 0;
            left: 50%;
            transform: translate(-50%);
            padding: 0 20px;
            width: 80%;
            box-sizing: border-box;

            .login-btn{
                width: 100%;
            }
        }
    }
}
</style>
